package CutLoad.mc
{
	import flash.display.MovieClip;
	import flash.events.Event;

	/**
	 * 正序或倒序读秒
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */
	public class McSecondsNumbers
	{
		private var mcNum:McNumberes;
		//如果有小数位数
		private var mcPNum:McNumberes;
		private var secondsCount:Number;
		private var timeI:Number
		private var timeCount:Number;
		private var current:Number;
		private var isStart:Boolean=false;
		//正序还中倒序
		private var isGogo:Boolean=true;
		//
		private var funEnd:Function;
		//
		/**
		 * 初始化 
		 * @param target 目标影片，详见:McNumberes类初始化的说明
		 * @param count 数子长度，，详见:McNumberes类初始化的说明
		 * @param isSpaceZero 空出的位数是否用零表示，详见:McNumberes类初始化的说明
		 * @param layoutMcNameFirst影片实例的名字 前缀部分，详见:McNumberes类初始化的说明
		 * 
		 */		
		public function McSecondsNumbers(target:MovieClip,count:Number,isSpaceZero:Boolean=true,layoutMcNameFirst:String="mc",PrecisionCount:Number=0,PrecisionMcNameFirst:String="mcs")
		{
			mcNum=new McNumberes(target,1,count,isSpaceZero,layoutMcNameFirst);
			mcPNum=null;
			if(PrecisionCount!=0) mcPNum=new McNumberes(target,1,PrecisionCount,isSpaceZero,PrecisionMcNameFirst);
		}
		/**
		 * 开始 
		 * @param secondsCount 开始的秒数
		 * @param isGogo 是正序还是倒序  默认是true:正序
		 * 
		 */		
		public function start(secondsCount:Number,isGogo:Boolean=true):void{
			this.isGogo=isGogo;
			this.secondsCount=secondsCount;
			timeI=0;
			timeCount=mcNum.mcTarget.stage.frameRate;
			isStart=true;
			mcNum.mcTarget.addEventListener(Event.ENTER_FRAME,enterFrame);
			current=secondsCount;
			mcNum.start(current);
		}
		/**
		 * 按照之前的设置，重新开始 
		 * 
		 */		
		public function reStart():void{
			this.start(secondsCount,isGogo);
		}
		/**
		 * 停止 
		 * 
		 */		
		public function stop():void{
			isStart=false;
		}
		//循环
		private function enterFrame(e:Event):void{
			if(!isStart) return;
			timeI++;
			if(mcPNum!=null){
				var tem123:Number=timeCount-timeI;
				tem123=(timeCount/60)*tem123;
				mcPNum.start(tem123);
			}
				if(timeI>=timeCount){
					timeI=0;
					mcPNum.start(0);
					mcPNum.start(timeI);
					if(!isGogo){
						//倒序
						current--;
						mcNum.start(current);
						if(current==0){
							isStart=false;
							if(funEnd!=null) funEnd();
						}
					}else{
						//正序
						current++;
						mcNum.start(current);
						if(current>=secondsCount){
							isStart=false;
							if(funEnd!=null) funEnd();
						}
					}
				}
		}
		/**
		 * 当前值 
		 * @return 
		 * 
		 */		
		public function get values():Number{
			return current;
		}
		/**
		 * 设置当结束时调用的额外的方法:格式：function():void 
		 * @param funEnd
		 * 
		 */		
		public function setFunEnd(funEnd:Function):void{
			this.funEnd=funEnd;
		}
		/**
		 * 作一些清除的处理
		 * 
		 */
		public function killMe():void{
			mcNum.mcTarget.removeEventListener(Event.ENTER_FRAME,enterFrame);
		}
	}
}
